﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AddIn.EA.Core.Common;
using AddIn.EA.Architecture.ComponentLogic;
using AddIn.EA.Framework;

namespace AddIn.EA.Architecture.Presentation
{
    [AddInMenu("Add Process Structure")]
    public partial class BuildProcessStructure : BaseAddInArchitecture
    {
        public BuildProcessStructure()
        {
            InitializeComponent();
            this.Structure = new ProcessStructure();
            processStructureBindingSource.DataSource = this.Structure;
            UpdateProcessFieldsState();
            this.TabOrder.Add(cboModule);
            this.TabOrder.Add(txtGeneralName);
            this.TabOrder.Add(chkRAPack);
            this.TabOrder.Add(chkComm);
            this.TabOrder.Add(txtName);
            this.TabOrder.Add(cboIntelligenceModule);
            this.TabOrder.Add(chkPE);
            this.TabOrder.Add(chkPL);
            this.TabOrder.Add(chkRA);
            this.TabOrder.Add(btnAdd);
            this.TabOrder.Add(btnRemove);
            this.TabOrder.Add(dataGridView1);
            this.TabOrder.Add(OK);
        }

        public ProcessStructure Structure { get; set; }

        private void OK_Click(object sender, EventArgs e)
        {
            if(ValidateProcessStructure())
                this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            processesBindingSource.AddNew();
            UpdateProcessFieldsState();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            processesBindingSource.Remove(processesBindingSource.Current);
            UpdateProcessFieldsState();
        }

        private void UpdateProcessFieldsState()
        {
            txtName.Enabled = (this.Structure.Processes.Count > 0);
            cboIntelligenceModule.Enabled = (this.Structure.Processes.Count > 0);

            if (this.Structure.Processes.Count == 0)
                cboIntelligenceModule.SelectedIndex = -1;
        }

        public bool ValidateProcessStructure()
        {
            if (String.IsNullOrEmpty(cboIntelligenceModule.Text))
            {
                MessageBox.Show("Please, select a module!", "Module not selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                cboIntelligenceModule.Focus();
                return false;
            }

            if (String.IsNullOrEmpty(txtGeneralName.Text))
            {
                MessageBox.Show("Please, fill the general name!", "General name not informed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtGeneralName.Focus();
                return false;
            }

            return ValidateProcesses();
        }

        public bool ValidateProcesses()
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells[0].Value == null || row.Cells[0].Value.ToString().Trim().Length == 0)
                {
                    MessageBox.Show("Please, fill the process name!", "Process name not informed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    row.Selected = true;
                    txtName.Focus();
                    return false;
                }

                if (String.IsNullOrEmpty(row.Cells[1].Value.ToString()))
                {
                    MessageBox.Show("Please, select a intelligence module!", "Intelligence Module not selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    row.Selected = true;
                    cboIntelligenceModule.Focus();
                    return false;
                }
            }

            return true;
        }

        protected override bool UpdateEA(RepositoryManager repository)
        {
            bool updated = base.UpdateEA(repository);

            if (!updated && repository != null)
            {
                updated = this.Structure.UpdateEA((Package)repository.GetSelectedObject());
            }

            return updated;
        }

        protected override bool CheckShowForm(AddIn.EA.Framework.Common.IElement selectedElement, MenuLocation location)
        {
            return (selectedElement is Package && ((Package)selectedElement).IsModel);
        }

        private void txtGeneralName_Leave(object sender, EventArgs e)
        {
            if (this.Structure.Processes.Count == 0 && !String.IsNullOrEmpty(txtGeneralName.Text))
            {
                this.Structure.Processes.Add(new ProcessObjects() { Name = txtGeneralName.Text });
                UpdateProcessFieldsState();
                cboIntelligenceModule.Focus();
            }
        }

        private void chkRAPack_CheckedChanged(object sender, EventArgs e)
        {
            chkRA.Enabled = chkRAPack.Enabled;
        }
    }
}
